<template>
  <div>
    <div style="background: white; min-width: 500px;">
      <div class="tableTitle" style="padding-bottom: 20px;">
        <span>{{tableTitle}}</span>
        <el-button v-show="showButton" style="float: right;margin-top: -10px; margin-right: 20px;"
        @click="removeDepartment">调整部门</el-button>
        <el-button style="float: right;margin-top: -10px; margin-right: 20px;" @click="addMember">添加成员</el-button>
      </div>
    </div>
    <el-table
            ref="multipleTable"
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%; min-width: 500px;"
            @selection-change="handleSelectionChange"
            @select="showButtonFunc"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column
              type="selection"
              style="width:20%">
      </el-table-column>
      <el-table-column
              prop="name"
              label="姓名"
              style="width:20%"
              show-overflow-tooltip>
      </el-table-column>
      <el-table-column
              prop="employee_id"
              label="工号"
              style="width:20%"
              show-overflow-tooltip>
      </el-table-column>
      <el-table-column
              prop="phone"
              label="手机"
              style="width:20%"
              show-overflow-tooltip>
      </el-table-column>
      <el-table-column
              prop="email"
              label="邮箱"
              style="width:20%"
              show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="编辑" style="width:20%">
        <template slot-scope="scope">
          <i class="el-icon-s-order" @click="handleEdit(scope.$index, scope.row)" style="cursor: pointer"></i>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
            title="修改成员"
            :visible.sync="memberDialog"
            width="500px"
            center
            :modal='false'>
      <el-form :model="updateDepartmentForm"  :rules="addDepartmentFormRules" ref="updateDepartmentForm" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model.trim="updateDepartmentForm.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model.trim="updateDepartmentForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model.trim="updateDepartmentForm.phone" placeholder="请输入手机"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model.trim="updateDepartmentForm.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model.trim="updateDepartmentForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="工号" prop="number">
          <el-input v-model.trim="updateDepartmentForm.number" placeholder="请输入工号"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="department" >
          <el-select v-model="updateDepartmentForm.department" filterable placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in departments"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位" prop="post">
          <el-select v-model="updateDepartmentForm.post" filterable placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in posts"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="updateMember('updateDepartmentForm')">确定</el-button>
          <el-button @click="memberDialog = false">取消</el-button>
          <el-button type="danger" style="margin-left: 150px;" @click="deleteMember">禁用</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog
            title="调整所在部门"
            :visible.sync="moveDepartmentDialog"
            width="500px"
            center
            :modal='false'>
      <el-select v-model="department" filterable placeholder="请选择" style="width: 100%">
        <el-option
          v-for="item in departments"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
                        <el-button type="primary">确定</el-button>
                        <el-button @click="moveDepartmentDialog = false">取消</el-button>
                        </span>
    </el-dialog>
    <el-dialog
            title="添加成员"
            :visible.sync="addMemberDialog"
            width="500px"
            center
            :modal='false'>
      <el-form :model="addDepartmentForm"  :rules="addDepartmentFormRules" ref="addDepartmentForm" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model.trim="addDepartmentForm.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model.trim="addDepartmentForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="phone">
          <el-input v-model.trim="addDepartmentForm.phone" placeholder="请输入手机"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model.trim="addDepartmentForm.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model.trim="addDepartmentForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="工号" prop="number">
          <el-input v-model.trim="addDepartmentForm.number" placeholder="请输入工号"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="department" >
          <el-select v-model="addDepartmentForm.department" filterable placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in departments"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位" prop="post">
          <el-select v-model="addDepartmentForm.post" filterable placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in posts"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="createMember('addDepartmentForm')">确定</el-button>
          <el-button @click="addMemberDialog = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import axios from "axios";

  export default {
    name: "memberTable",
    props: ['tableTitle','tableData'],
    data() {
      let validateNumber = (rule, value, callback) => {
        let reg = /^[a-zA-Z0-9]+$/
        if(!reg.test(value)){
          callback(new Error('只能输入字母和数字'))
        }else{
          callback();
        }
      };
      let validatePhone = (rule, value, callback) => {
        let reg = /^1[345789]\d{9}$/
        if(!reg.test(value)){
          callback(new Error('请输入格式正确的手机号码'))
        }else{
          callback();
        }
      };
      let validateEmail = (rule, value, callback) => {
        let reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
        if(!reg.test(value)){
          callback(new Error('请输入格式正确的邮箱'))
        }else{
          callback();
        }
      };
      return {
        memberDialog: false,
        showButton: false,
        moveDepartmentDialog: false,
        addMemberDialog: false,
        department:'',
        multipleSelection: [],
        addDepartmentForm:{
          name:'',
          username:'',
          password:'',
          phone:'',
          email:'',
          number:'',
          department:'',
          post:'',
          enterpriseId:0,
        },
        updateDepartmentForm:{
          id:'',
          name:'',
          username:'',
          password:'',
          phone:'',
          email:'',
          number:'',
          department:'',
          post:'',
          enterpriseId:0,
        },
        departments:[],
        posts:[],
        addDepartmentFormRules:{
          name: [
            { required: true, message: "请输入姓名", trigger: 'blur'},
          ],
          username: [
            { required: true, message: '请选择用户名',trigger:'blur'},
          ],
          password: [
            { required: true, message: '请选择密码',trigger:'blur'}
          ],
          phone: [
            { required: true, message: '请选择手机号',trigger:'blur'},
            { validator: validatePhone , trigger: 'blur'}
          ],
          email: [
            { required: true, message: "请选择邮箱", trigger: 'blur'},
            { validator: validateEmail , trigger: 'blur'}
          ],
          number: [
            { required: true, message: "请选择工号", trigger: 'blur'},
            { validator: validateNumber , trigger: 'blur'}
          ],
          department: [
            { required: true, message: "请选择部门", trigger: 'change'}
          ],
          post: [
            { required: true, message: '请选择职位',trigger:'change'},
          ],}
      }
    },

    created() {
        axios.post('/getAllUser',{'id': window.localStorage.getItem("enterprise_id")}).then(res => {
          this.$parent.$parent.$parent.tableData = res.data;
        })
        axios.post('/getDepartment',{'id': window.localStorage.getItem("enterprise_id")}).then(res => {
          this.departments = res.data;
        })
        axios.post('/getPost',{'id': window.localStorage.getItem("enterprise_id")}).then(res => {
          this.posts = res.data;
        })
        this.addDepartmentForm.enterpriseId = window.localStorage.getItem("enterprise_id");
    },

    methods: {
      handleSelectionChange(selection) {
        if(selection.length > 0){
          this.showButton = true;
        }else{
          this.showButton = false;
        }
      },
      handleEdit(index, row) {
        this.updateDepartmentForm.name = row.name;
        this.updateDepartmentForm.username = row.username;
        this.updateDepartmentForm.password = row.password;
        this.updateDepartmentForm.email = row.email;
        this.updateDepartmentForm.phone = row.phone;
        this.updateDepartmentForm.department = row.department_id;
        this.updateDepartmentForm.post = row.post_id;
        this.updateDepartmentForm.number = row.employee_id;
        this.updateDepartmentForm.enterpriseId = window.localStorage.getItem("enterprise_id");
        this.updateDepartmentForm.id = row.id;
        this.memberDialog = true;
      },
      showButtonFunc(selection) {
        if(selection.length > 0){
          this.showButton = true;
        }else{
          this.showButton = false;
        }
      },
      removeDepartment(){
          this.moveDepartmentDialog = true;
      },
      addMember(){
          this.addMemberDialog = true;
      },
      createMember(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post("/createMember",this.addDepartmentForm).then(res => {
              switch (res.data) {
                case 0:
                  this.$message.error("系统已存在该用户名");
                  break;
                case 1:
                  this.$message.error("系统已存在该手机号");
                  break;
                case 2:
                  this.$message.error("系统已存在该邮箱");
                  break;
                case 3:
                  this.$message.error("系统已存在该员工编号");
                  break;
                case 4:
                  this.$message.success("成功创建");
                  this.addMemberDialog =false;
                  this.$refs["addDepartmentForm"].resetFields();
                  break;
              }
            })
          }
        })
      },

      updateMember(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios.post("/updateMember",this.updateDepartmentForm).then(res => {
              switch (res.data) {
                case 0:
                  this.$message.error("系统已存在该用户名");
                  break;
                case 1:
                  this.$message.error("系统已存在该手机号");
                  break;
                case 2:
                  this.$message.error("系统已存在该邮箱");
                  break;
                case 3:
                  this.$message.error("系统已存在该员工编号");
                  break;
                case 4:
                  this.$message.success("成功修改");
                  this.memberDialog =false;
                  break;
              }
            })
          }
        })
      },
      deleteMember(){
        axios.post("/disableUser",{"username":this.updateDepartmentForm.username}).then(res => {
          if(res.data == true){
            this.$message.success("禁用成功");
            this.memberDialog = false;
          }else{
            this.$message.error("禁用失败");
          }
        })
      }
    }
  }
</script>

<style>
  .tableTitle {
    font-size: 25px;
    padding-left: 10px;
    padding-top: 20px;
  }
</style>
